//
//  XLEmoticonTools.swift
//  XLVeibo
//
//  Created by macbook on 16/7/4.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

private let tagNum = 1000
enum XLEmotionToolsType:Int {
        /// mor
    case Nomer = 0
     /// Emo表情
    case emoji = 1
     /// 浪小花
    case Lxh = 2
    }

class XLEmoticonTools: UIView {

    
    var currentBtn:UIButton?
    //声明一个闭包，告诉外界点击的那个按钮
    var toolbarClosure:((type:XLEmotionToolsType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    //给外界提供一个方法
    func setCurrentButtons(tag: Int){
    
        //通过tag获取btn
        let button = viewWithTag(tag + tagNum) as!UIButton
        
        if currentBtn == button {
            
            return
        }
        button.selected = true
        currentBtn?.selected = false
        currentBtn = button
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    backgroundColor = UIColor.greenColor()
        
        let leftBtn = setupBtn("left", title: "默认", emogjiType: .Nomer)
        let midBtn = setupBtn("mid", title: "Emoji", emogjiType: .emoji)
        let rightBtn = setupBtn("right", title: "浪小花", emogjiType: .Lxh)
        
        leftBtn.snp_makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(midBtn)
        }
        
        midBtn.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.left.equalTo(leftBtn.snp_right)
            make.width.equalTo(rightBtn)
        }
        
        rightBtn.snp_makeConstraints { (make) in
            
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(midBtn.snp_right)
        }
        
        leftBtn.selected = true
        
        currentBtn?.selected = false
        
        currentBtn = leftBtn
       
        
    }
    
    
    //按钮方法的实现
    func btnClick(button:UIButton){
    
        if currentBtn == button {
            
            return
        }
        
        
        button.selected = true
        
        currentBtn?.selected = false

        currentBtn = button
        
        //执行闭包
        toolbarClosure?(type: XLEmotionToolsType(rawValue: button.tag - tagNum)!)
    }
    
    //提供创建but的方法 compose_emotion_table_left_normal
    func setupBtn(image:String,title:String,emogjiType:XLEmotionToolsType)->UIButton{
    
        let btn = UIButton()
        btn.tag = emogjiType.rawValue + tagNum
        
        //添加点击方法
        btn.addTarget(self, action:#selector(XLEmoticonTools.btnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
        
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(image)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(image)_selected"), forState: UIControlState.Selected)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        //添加
        self.addSubview(btn)
         return btn
    }
    
}









